home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -in_the_mag- / emulation / macos / uae069b2.src.cpt.hqx / UAE069ß2.SRC.CPT / uae069fl2.src / debug.c < prev    next >
C/C++ Source or Header  |  1997-06-13  |  7KB  |  307 lines

  1.  /* 
  2.   * UAE - The Un*x Amiga Emulator
  3.   * 
  4.   * Debugger
  5.   * 
  6.   * (c) 1995 Bernd Schmidt
  7.   * 
  8.   */
  9.  
  10. #include "sysconfig.h"
  11. #include "sysdeps.h"
  12.  
  13. #include <ctype.h>
  14. #include <signal.h>
  15.  
  16. #include "config.h"
  17. #include "options.h"
  18. #include "uae.h"
  19. #include "my_memory.h"
  20. #include "custom.h"
  21. #include "readcpu.h"
  22. #include "newcpu.h"
  23. #include "debug.h"
  24. #include "cia.h"
  25. #include "xwin.h"
  26. #include "gui.h"
  27.  
  28. static int debugger_active = 0;
  29. static uaecptr skipaddr;
  30. static int do_skip;
  31. int debugging = 0;
  32.  
  33. void activate_debugger(void)
  34. {
  35.     do_skip = 0;
  36.     if (debugger_active)
  37.     return;
  38.     debugger_active = 1;
  39.     regs.spcflags |= SPCFLAG_BRK;
  40.     debugging = 1;
  41.     use_debugger = 1;
  42. }
  43.  
  44. int firsthist = 0;
  45. int lasthist = 0;
  46. #ifdef NEED_TO_DEBUG_BADLY
  47. struct regstruct history[MAX_HIST];
  48. union flagu historyf[MAX_HIST];
  49. #else
  50. uaecptr history[MAX_HIST];
  51. #endif
  52.  
  53. static void ignore_ws(char **c)
  54. {
  55.     while (**c && isspace(**c)) (*c)++;
  56. }
  57.  
  58. static uae_u32 readhex(char **c)
  59. {
  60.     uae_u32 val = 0;
  61.     char nc;
  62.  
  63.     ignore_ws(c);
  64.     
  65.     while (isxdigit(nc = **c)){
  66.     (*c)++;
  67.     val *= 16;
  68.     nc = toupper(nc);
  69.     if (isdigit(nc)) {
  70.         val += nc - '0';
  71.     } else {
  72.         val += nc - 'A' + 10;
  73.     }
  74.     }
  75.     return val;
  76. }
  77.  
  78. static char next_char(char **c)
  79. {
  80.     ignore_ws(c);
  81.     return *(*c)++;
  82. }
  83.  
  84. static int more_params(char **c)
  85. {
  86.     ignore_ws(c);
  87.     return (**c) != 0;
  88. }
  89.  
  90. static void dumpmem(uaecptr addr, uaecptr *nxmem, int lines)
  91. {
  92.     broken_in = 0;
  93.     for (;lines-- && !broken_in;){
  94.     int i;
  95.     printf("%08lx ", addr);
  96.     for(i=0; i< 16; i++) {
  97.         printf("%04x ", get_word(addr)); addr += 2;
  98.     }
  99.     printf("\n");
  100.     }
  101.     *nxmem = addr;
  102. }
  103.  
  104. static void modulesearch(void)
  105. {
  106.     uae_u32 ptr;
  107.     
  108.     for(ptr=0x438;ptr<chipmem_size;ptr+=4) {
  109.     /* Check for Mahoney & Kaktus */
  110.     /* Anyone got the format of old 15 Sample (SoundTracker)modules? */
  111.     if(chipmemory[ptr] == 'M' && chipmemory[ptr+1] == '.'
  112.        && chipmemory[ptr+2] == 'K' && chipmemory[ptr+3] == '.')
  113.     {
  114.         char name[21];
  115.         uae_u8 *ptr2 = &chipmemory[ptr-0x438];
  116.         int i,j,length;
  117.  
  118.         printf("Found possible ProTracker (31 samples) module at 0x%lx.\n",ptr-0x438);
  119.         memcpy(name,ptr2,20);
  120.         name[20] = '\0';
  121.         
  122.         /* Browse playlist */
  123.         ptr2 += 0x3b8;
  124.         i = ptr2[-2]; /* length of playlist */
  125.         length = 0;
  126.         while(i--)
  127.         if((j=*ptr2++)>length)
  128.             length=j;
  129.         
  130.         length = (length+1)*1024+0x43c;
  131.         
  132.         /* Add sample lengths */
  133.         ptr2 = &chipmemory[ptr-0x438+0x2a];
  134.         for(i=0;i<31;i++,ptr2+=30)
  135.         length += 2*((ptr2[0]<<8)+ptr2[1]);
  136.         
  137.         printf("Name \"%s\", Length %ld (0x%lx) bytes.\n", name, length, length);
  138.     }
  139.     }
  140. }
  141.  
  142. void debug(void)
  143. {
  144.     char input[80],c;
  145.     uaecptr nextpc,nxdis,nxmem;
  146.  
  147.     bogusframe = 1;
  148.     
  149.     if (do_skip && (m68k_getpc() != skipaddr/* || regs.a[0] != 0x1e558*/)) {
  150.     regs.spcflags |= SPCFLAG_BRK;
  151.     return;
  152.     }
  153.     do_skip = 0;
  154.  
  155. #ifdef NEED_TO_DEBUG_BADLY
  156.     history[lasthist] = regs;
  157.     historyf[lasthist] = regflags;
  158. #else
  159.     history[lasthist] = m68k_getpc();
  160. #endif
  161.     if (++lasthist == MAX_HIST) lasthist = 0;
  162.     if (lasthist == firsthist) {
  163.     if (++firsthist == MAX_HIST) firsthist = 0;
  164.     }
  165.  
  166.     m68k_dumpstate(&nextpc);
  167.     nxdis = nextpc; nxmem = 0;
  168.  
  169.     for(;;){
  170.     char cmd,*inptr;
  171.     
  172.     printf(">");
  173.     fflush (stdout);
  174.     if (fgets(input, 80, stdin) == 0)
  175.         return;
  176.     inptr = input;
  177.     cmd = next_char(&inptr);
  178.     switch(cmd){
  179.      case 'c': dumpcia(); dumpcustom(); break;
  180.      case 'r': m68k_dumpstate(&nextpc); break;
  181.      case 'M': modulesearch(); break;
  182.      case 'd': 
  183.         {
  184.         uae_u32 daddr;
  185.         int count;
  186.         
  187.         if (more_params(&inptr))
  188.             daddr = readhex(&inptr);
  189.         else 
  190.             daddr = nxdis;
  191.         if (more_params(&inptr))
  192.             count = readhex(&inptr); 
  193.         else
  194.             count = 10;
  195.         m68k_disasm(daddr, &nxdis, count);
  196.         }
  197.         break;
  198.      case 't': regs.spcflags |= SPCFLAG_BRK; return;
  199.      case 'z':
  200.         skipaddr = nextpc;
  201.         do_skip = 1;
  202.         regs.spcflags |= SPCFLAG_BRK;
  203.         return;
  204.  
  205.      case 'f': 
  206.         skipaddr = readhex(&inptr);
  207.         do_skip = 1;
  208.         regs.spcflags |= SPCFLAG_BRK;
  209.         return;
  210.  
  211.      case 'q': uae_quit();
  212.         debugger_active = 0;
  213.         debugging = 0;
  214.         return;
  215.  
  216.      case 'g':
  217.         if (more_params (&inptr))
  218.         m68k_setpc (readhex (&inptr));
  219.         debugger_active = 0;
  220.         debugging = 0;
  221.         return;
  222.  
  223.      case 'H':
  224.         {
  225.         int count;
  226.         int temp;
  227. #ifdef NEED_TO_DEBUG_BADLY
  228.         struct regstruct save_regs = regs;
  229.         union flagu save_flags = regflags;
  230. #endif
  231.  
  232.             if (more_params(&inptr))
  233.             count = readhex(&inptr); 
  234.             else
  235.             count = 10;
  236.             if (count < 0)
  237.             break;
  238.             temp = lasthist;
  239.             while (count-- > 0 && temp != firsthist) {
  240.             if (temp == 0) temp = MAX_HIST-1; else temp--;
  241.             }
  242.             while (temp != lasthist) {
  243. #ifdef NEED_TO_DEBUG_BADLY
  244.             regs = history[temp];
  245.             regflags = historyf[temp];
  246.             m68k_dumpstate(NULL);
  247. #else
  248.             m68k_disasm(history[temp], NULL, 1);
  249. #endif
  250.             if (++temp == MAX_HIST) temp = 0;
  251.             }
  252. #ifdef NEED_TO_DEBUG_BADLY
  253.         regs = save_regs;
  254.         regflags = save_flags;
  255. #endif
  256.         }
  257.         break;
  258.      case 'm':
  259.         {
  260.         uae_u32 maddr; int lines;
  261.         if (more_params(&inptr))
  262.             maddr = readhex(&inptr); 
  263.         else 
  264.             maddr = nxmem;
  265.         if (more_params(&inptr))
  266.             lines = readhex(&inptr); 
  267.         else 
  268.             lines = 16;
  269.         dumpmem(maddr, &nxmem, lines);
  270.         }
  271.         break;
  272.           case 'h':
  273.           case '?':
  274.         {
  275.              printf ("          HELP for UAE Debugger\n");
  276.              printf ("         -----------------------\n\n");
  277.              printf ("  g: <address>          ");
  278.                 printf("Start execution at the current address or <address>\n");
  279.              printf ("  c:                    ");
  280.                 printf("Dump state of the CIA and custom chips\n");
  281.              printf ("  r:                    ");
  282.                 printf("Dump state of the CPU\n");
  283.              printf ("  m <address> <lines>:  ");
  284.                 printf ("Memory dump starting at <address>\n");
  285.              printf ("  d <address> <lines>:  ");
  286.                 printf ("Disassembly starting at <address>\n");
  287.              printf ("  t:                    ");
  288.                 printf ("Step one instruction\n");
  289.              printf ("  z:                    ");
  290.                 printf ("Step through one instruction - useful for JSR, DBRA etc\n");
  291.              printf ("  f <address>:          ");
  292.                 printf ("Step forward until PC == <address>\n");
  293.              printf ("  H <count>:            ");
  294.                 printf ("Show PC history <count> instructions\n");
  295.              printf ("  M:                   ");
  296.                 printf ("Search for *Tracker sound modules\n");
  297.              printf ("  h,?:                  ");
  298.                 printf ("Show this help page\n");
  299.              printf ("  q:                    ");
  300.                 printf ("Quit the emulator. You don't want to use this command.\n\n");
  301.             }
  302.             break;
  303.              
  304.     }
  305.     }
  306. }
  307.